﻿@{
    ViewBag.Title = "组件过站配置";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var orderNo = ViewBag.OrderNo as string;
    var workOrderNo = ViewBag.WorkOrderNo as string;
    var sites = ViewBag.Sites as List<KeyValuePair<IOA.MES.DataAccess.Enum.SiteType, string>>;
    var config = ViewBag.Config as IOA.MES.DataAccess.VModel.VViaSiteConfig;
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div style="background-color:white;padding:10px;margin:15px">
        <div class="layui-form">
            @foreach (var site in sites)
            {
                <div class="layui-form-item viasite" sitetype="@((int)site.Key)">
                    <label class="layui-form-label">@site.Value</label>
                    <div class="layui-input-block">
                        <input type="checkbox" lay-skin="primary" title="可重复过站" class="MultipleVia" @(config.ConfigModel.ViaSites.FirstOrDefault(_ => _.SiteType == site.Key)?.MultipleVia ?? false ? "checked" : "")>
                        <input type="checkbox" lay-skin="primary" title="必须过站" class="ForceVia" @(config.ConfigModel.ViaSites.FirstOrDefault(_ => _.SiteType == site.Key)?.ForceVia ?? false ? "checked" : "")>
                    </div>
                </div>
            }

            <div class="layui-form-item" pane="">
                <label class="layui-form-label">装框</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="JunctionBoxPhoto" lay-skin="primary" title="接线盒拍照" @(config.ConfigModel.Framing.JunctionBoxPhoto ? "checked" : "")>
                    <input type="checkbox" name="JunctionBoxBarCodeScan" lay-skin="primary" title="接线盒需扫码" @(config.ConfigModel.Framing.JunctionBoxBarCodeScan ? "checked" : "")>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn " lay-submit lay-filter="submit">{{lang.Field.Submit}}</button>
                </div>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var orderNo = "@ViewBag.OrderNo",
            workOrderNo = "@ViewBag.WorkOrderNo";
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['form', 'layer'], function () {
                var layer = layui.layer,
                    form = layui.form;

                form.on('submit(submit)', function (data) {
                    console.log(data.field);

                    var viaSites = new Array();
                    $(".viasite").each(function () {
                        viaSites.push({
                            SiteType: parseInt($(this).attr("sitetype")),
                            MultipleVia: $(this).find(".MultipleVia")[0].checked,
                            ForceVia: $(this).find(".ForceVia")[0].checked
                        });
                    });

                    var loadId = layer.load();
                    $.ajax({
                        async: false,
                        url: "/ParaSetting/EditViaSiteConfig",
                        data: {
                            model: {
                                OrderNo: orderNo,
                                WorkOrderNo: workOrderNo,
                                ConfigModel: {
                                    ViaSites: viaSites,
                                    Framing: {
                                        JunctionBoxPhoto: $("input[name=JunctionBoxPhoto]")[0].checked,
                                        JunctionBoxBarCodeScan: $("input[name=JunctionBoxBarCodeScan]")[0].checked
                                    }
                                }
                            }
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            layer.msg("保存成功！", { icon: 6, time: 1500 });
                        }
                    });
                    return false;
                });
            });
        }
    </script>
}
